package com.adam.springbucks.controller;

import com.adam.springbucks.entity.Person;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class Q2Controller {

    /**
     * 返回json数据格式
     * @param id
     * @return
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    public @ResponseBody
    Person getPersonJson(Long id){
        return personData(id);
    }

    /**
     * 返回xml数据格式
     * @param id
     * @return
     */
    @RequestMapping(value = "/xml", method = RequestMethod.GET
            , produces = "text/xml; charset=utf-8")
    public @ResponseBody Person getPersonXml(Long id) {
        return personData(id);
    }

    private Person personData(Long id){
        List<Person> personList = new ArrayList<>();
        Person person = new Person(1L,"Messi",35);
        Person person2 = new Person(2L,"Kaka",28);
        Person person3 = new Person(3L,"Cafu",22);
        Person person4 = new Person(4L,"Dida",21);
        Person person5 = new Person(5L,"Figa",40);
        personList.add(person);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        personList.add(person5);
        personList.add(person);
        for (Person p: personList) {
            if (p.getId().equals(id)){
                System.out.println("p:" + p);
                return p;
            }
        }
        return new Person(100L,"dada",32);
    }

}
